home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 0022-3.564 / dmg-3557 / news.txt / asmtut.asc < prev    next >
Text File  |  1989-04-05  |  10KB  |  278 lines

  1.  
  2.                          #######################
  3.                          #                     #
  4.                          #  Assembly Tutorial  #
  5.                          #  ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯  # 
  6.                          #    by Duncan and    #
  7.                          #    Robert Annett    #
  8.                          #                     #
  9.                          #######################
  10.  
  11.  
  12. This article should have appeared in Stupendous several months ago but  as 
  13. it hasn't I will let STEN readers have a read of it.  
  14.  
  15. I  hope  you  like it.  If you do then perhaps I  could  do  some  regular 
  16. assembler  stuff in STEN.  This was mostly written by Duncan  Annett  with 
  17. this introduction and some bits and pieces by me,  Robert Annett.  
  18.  
  19. So here goes....
  20.  
  21. One of the best ways to learn a language is to follow an example program, 
  22. below is one such example.  The program asks the user (i.e. you!) to input 
  23. the name of a Degas picture file and then loads it onto the screen.
  24.  
  25. First of all a quick note for when you assemble and run the program:- 
  26.  
  27.         
  28.         -      assemble the program to disk giving it a .TOS extension.
  29.  
  30.         -      run the program in low resolution to load a  .PI1 
  31.                picture.
  32.  
  33.         -      the picture must be in the same directory.
  34.  
  35.  
  36.       (The program listing may also be found in the PROGRAMS folder)
  37.  
  38.  
  39.                       ****Picture Loader****
  40.  
  41. --------------------------------------------------------------------------
  42.  
  43. startup         move.l 4(sp),a3 
  44.                 move.l $c(a3),d0        ;length of text
  45.                 add.l $14(a3),d0        ;length of data constants
  46.                 add.l $1c(a3),d0        ;length of data storage
  47.                 add.l #500,d0           ;additional memory
  48.                 add.l #$100,d0          ;length of basepage
  49.                 move.l a7,oldstack
  50.                 lea endstack(pc),a7
  51.                 move.l d0,-(a7)
  52.                 move.l a3,-(a7)
  53.                 clr -(a7)
  54.                 move #$4a,-(a7)
  55.                 trap #1
  56.                 lea 12(sp),sp
  57.  
  58.  
  59. When a program is run,  it grabs all free memory for itself. Normally this 
  60. does  not matter,  however if your program uses any functions that  manage 
  61. the memory,  for example allocating a memory block or disk functions, then 
  62. some memory has to be given back to the system.  It is good pratice to  do 
  63. this in whatever program you write.  This is what the above section  does. 
  64. It  calculates the length of the program in D0,  saves the address  of  A7 
  65. (also called the stack pointer [SP]) at the location oldstack,  places the 
  66. position of A7 in our program (at location endstack) and then performs the 
  67. neccessary TRAP call.
  68.  
  69. Trap calls are routines stored in the Atari's ROM and are very  useful.For 
  70. instance,  if  a  computers' hardware changes like the STFM and  STE  then 
  71. programs  should  still  work as the TRAP call will  handle  the  hardware 
  72. changes.  If  programmers  get  fancy and write  programs  to  access  the 
  73. hardware directly,  then it will fail to work on the new hardware, this is 
  74. what happened with the STE.
  75.  
  76. Another fact about the TRAP is that they destroy the contents of registers 
  77. D0-D2  and A0-A2,  so if valuable data is stored in them then  save  those 
  78. registers.  Also  remember  to  correct the stack.This  is  the  line  lea 
  79. 12(sp),sp this adds 12 to the stack pointer (A7) and stops it from writing 
  80. over our program.
  81.  
  82.  
  83. --------------------------------------------------------------------------
  84.  
  85. setstuff        move.l #0,-(sp)         ;0 - low  1-med  2-high
  86.                 move.l #$78000,-(sp)
  87.                 move.l #$78000,-(sp)
  88.                 move #5,-(sp)
  89.                 trap #14
  90.                 add.l #14,sp
  91.  
  92.  
  93. This section sets up the screen.  The first line changes to low resolution 
  94. (this  only  works for TOS programs,  GEM will not adjust to  the  correct 
  95. resoltion,  this  is why the program is assembled to .TOS).  The next  two 
  96. lines are the addresses of the LOGICAL and PHYSICAL screens. I will not go 
  97. explain  these  in  this article but just assume they are  both  the  same 
  98. (their address must be on a 256 byte boundary (STFMs' only,  STEs' can  be 
  99. on any even boundary))
  100.  
  101.  
  102. --------------------------------------------------------------------------
  103.  
  104. printline       move.l #text,-(sp)
  105.                 move #9,-(sp)
  106.                 trap #1
  107.                 add.l #6,sp
  108.  
  109.  
  110. This  section  prints a message on the screen.This message  is  stored  at 
  111. location  text (the # sign means 'the address of',  if there was no #  and 
  112. just  'text' it means 'the data stored in the address of').  This  message 
  113. can be changed to whatever you want and can incoporate VT52 control  codes 
  114. (they  would be placed outside of the quotes,  seperated  by  commas,  and 
  115. before the final 0).
  116.  
  117. The text must always have a null character at the end, this is simply a 0. 
  118.  
  119.  
  120. --------------------------------------------------------------------------
  121.  
  122. input           move.l #inpstart,-(sp)
  123.                 move #$a,-(sp)
  124.                 trap #1
  125.                 add.l #6,sp
  126.                 lea buff(pc),a1
  127.                 add.w d0,a1
  128.                 move.b #0,(a1) 
  129.  
  130.  
  131. This section asks the user for an input. The first line places the address 
  132. of  a buffer to the stack.  The first two bytes are  reserved,  the  first 
  133. containing the maximum length of the string to be inputted and the  second 
  134. contains  the  actual length of string that was input (also  contained  in 
  135. D0).
  136.  
  137. The  last three lines place a null at the end of the input,  this  is  for 
  138. later use.  Basically,  A1 points to the start of the input data  (missing 
  139. out those first two bytes),  then the length of the string is added to  A1 
  140. and a zero is placed in this address.
  141.  
  142.  
  143. --------------------------------------------------------------------------
  144.  
  145. openfile        move #0,-(sp)     ;0-read,2-write,3-read and write
  146.                 move.l #buff,-(sp)
  147.                 move #$3d,-(sp)
  148.                 trap #1
  149.                 add.l #8,sp 
  150.                 tst d0
  151.                 bmi end
  152.                 move d0,handle
  153.  
  154.  
  155. This  opens  a  file.  The first line specifies which  operations  can  be 
  156. carried,  for this program we only want to read the file. Then the address 
  157. of  the filename is placed on the stack,  it has to be ended with  a  null 
  158. (this  part  was  carried out in the previous  section).  This  TRAP  call 
  159. returns a value in D0, if it is negative then an error occured (e.g. could 
  160. not  find  the file) if this happens the program jumps to the end  (bmi  - 
  161. branch if minus). If not then the value is saved.
  162.  
  163.  
  164. --------------------------------------------------------------------------
  165.  
  166. readpal         move.l #paldat,-(sp)
  167.                 move.l #34,-(sp)
  168.                 move handle,-(sp)
  169.                 move #$3f,-(sp)
  170.                 trap #1
  171.                 add.l #12,sp
  172. setpal          move.l #paldat+2,-(sp)  ;add 2 to get to palette
  173.                 move #6,-(sp)
  174.                 trap #14
  175.                 add.l #6,sp
  176.  
  177.  
  178. This  reads  in the colour palette.  Degas pictures have a  34  byte  long 
  179. header,  the  first word (two bytes) contains the resolution mode and  the 
  180. next 16 words contain the palette. #paldat is the address the colours will 
  181. be  loaded  to,  #34 is the number of bytes to be read and handle  is  the 
  182. value of the file number.
  183.  
  184. Then  the  next  TRAP  call loads the colours  into  the  hardware  colour 
  185. registers,  remembering  that  the palette data is two bytes in  from  the 
  186. start of the header.
  187.  
  188.  
  189. --------------------------------------------------------------------------
  190.  
  191. readpic         move.l physbase,-(sp)
  192.                 move.l #32000,-(sp)
  193.                 move handle,-(sp)
  194.                 move #$3f,-(sp)
  195.                 trap #1
  196.                 add.l #12,sp 
  197.                 tst d0
  198.                 bmi end
  199.  
  200.  
  201. Much  the same as before,  physbase is the address of the  screen  (notice 
  202. that  this places the data stored at physbase onto the stack).  Also  this 
  203. checks for errors D0 being negative if there are any. Remember the handle. 
  204. 32000 is the length of the picture data.
  205.  
  206.  
  207. --------------------------------------------------------------------------
  208.  
  209. closefile       move handle,-(sp)
  210.                 move #$3e,-(sp)
  211.                 trap #1
  212.                 add.l #4,sp
  213.                 tst d0
  214.                 beq wait
  215.  
  216.  
  217. This  closes the file.  All that has to be passed is the file  handle.  It 
  218. then waits for a key to be pressed.
  219.  
  220.  
  221. --------------------------------------------------------------------------
  222.  
  223. end             move.l oldstack,a7
  224.                 clr -(sp)
  225.                 trap #1
  226.  
  227.  
  228. This  reloads the stack pointer and terminates the program.  It  needs  no 
  229. stack correction because the program ends.
  230.  
  231.  
  232. --------------------------------------------------------------------------
  233.  
  234. wait            move #7,-(sp)
  235.                 trap #1
  236.                 add.l #2,sp
  237.                 bra end
  238.  
  239.  
  240. This waits for any key to be pressed and jumps to the end of the program.
  241.  
  242.  
  243. --------------------------------------------------------------------------
  244.  
  245.         even
  246.  
  247. newstack        ds.l 100
  248. endstack        dc.l 0
  249. oldstack        dc.l 0
  250. text            dc.b "Name of picture? ",0
  251.         even
  252. inpstart        dc.b 12,0          ;input 12 chars MAX.
  253. buff            ds.b 15            ;buffer for user input
  254.         even
  255. handle          dc.w 0
  256. physbase        dc.l $78000
  257. paldat          ds.w 17
  258.  
  259.  
  260. The data area.  DC means Declare Constant and stores that value in memory. 
  261. DS means Declare Storage and reserves a block of memory.
  262.  
  263.  
  264.                                 ~~~OOOO~~~
  265.  
  266. If you wish to contact Robert, then send your letters via myself at:
  267.  
  268.           Michael McLaughlin
  269.           152 Hapland Road
  270.           Pollok
  271.           Glasgow G53 5PR
  272.  
  273. If you want a reply then send a S.A.E.
  274.  
  275.  
  276.                                ___ooOoo___
  277.  
  278.